package com.tepestech.pfautocard.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.tepestech.pfautocard.R;

import java.util.ArrayList;


public class CharacterAdapter extends BaseAdapter {
    private ArrayList <PlayerCharacterInfo> list = new ArrayList<>();
    private LayoutInflater mInflater;

   public CharacterAdapter(Context context){
       mInflater = LayoutInflater.from(context);
   }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public PlayerCharacterInfo getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    static class ViewHolder
    {
         ImageView image;
         TextView name;
         TextView level;
    }
    //然后重写getView

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null)
        {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.character_list_item, null);
            holder.image = convertView.findViewById(R.id.character_image);
            holder.name =convertView.findViewById(R.id.character_name);
            holder.level = convertView.findViewById(R.id.character_level);
            convertView.setTag(holder);
        }else
        {
            holder = (ViewHolder)convertView.getTag();
        }
        holder.image.setImageDrawable(getItem(position).image);
        holder.name.setText(getItem(position).name);
        holder.level.setText(getItem(position).level);

    return convertView;
}

    public void setData(ArrayList<PlayerCharacterInfo> list) {
        this.list = list;
        notifyDataSetChanged();
    }
}
